# Contributor: gay <gay@disroot.org>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=darcs
pkgver=2.18.4
pkgrel=0
pkgdesc="Patch-based revision control system"
url="https://darcs.net/"
arch="aarch64 x86_64" # limited by ghc
license="GPL-2.0-or-later"
makedepends="cabal ghc curl-dev ncurses-dev zlib-dev"
#checkdepends="diffutils grep lighttpd musl-locales"
options="!check" # tests relying on presence of shell utils fail without giving detailed log
subpackages="$pkgname-doc"
source="https://hackage.haskell.org/package/darcs-$pkgver/darcs-$pkgver.tar.gz
	cabal.project.freeze
	"

# Directory where cabal files are stored.
export CABAL_DIR="${CABAL_DIR:-"$srcdir/dist"}"

# Needed to fix build on aarch64.
export PATH="$PATH:/usr/lib/llvm15/bin"

cabal_update() {
	local repo="hackage.haskell.org"

	# Default config uses HTTP, change it to HTTPS.
	[ -f "$CABAL_DIR"/config ] || {
		cabal user-config init
		cabal user-config update -a \
			"repository $repo {url: https://$repo/}"
	}

	cd "$startdir"
	[ -d "$builddir" ] || abuild unpack
	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.project.freeze with version constraints.
	(
		cd "$builddir" || {
			error 'Is $builddir set correctly?'
			return 1
		}
		cabal v2-update
		cabal v2-freeze --shadow-installed-packages

		mv -v cabal.project.freeze "$startdir"/
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	ln -svf "$srcdir"/cabal.project.freeze "$builddir"/
}

build() {
	cabal v2-update
	cabal v2-build darcs:exes \
		--jobs=${JOBS:-1} \
		--prefix=/usr \
		--docdir=/usr/share/doc/$pkgname \
		--sysconfdir=/etc
}

check() {
	cabal test
}

package() {
	cd dist-newstyle/build/*-linux/ghc-*/$pkgname-$pkgver/build/$pkgname
	install -Dm755 $pkgname "$pkgdir"/usr/bin/$pkgname
	install -Dm644 $pkgname.1 "$pkgdir"/usr/share/man/man1/$pkgname.1
}

sha512sums="
e2a44fe687b81272b8cadd908b30d0cbcbb55ab0fa8afb9c8c964c33703303c53fdd504287a51f32088e393954be83d2fb58ac644560471a78e5bc4ef7e6a8e2  darcs-2.18.4.tar.gz
d18cacf4f426265b6d13a096209e70e0bb9a4d2bf0572843c58cbf0bd713d06eddfdcf8885b049daa20ccf840decf38431074655fed14c408a22739a2c9d88a3  cabal.project.freeze
"
